// 创建用户相关的小仓库
import { defineStore } from "pinia";
import type { studentAllResponseData, studentInfoData, studentListFormData, studentListResponseData, studentResponseData } from "@/api/student/type";
import { reqStudentAdd, reqStudentAll, reqStudentById, reqStudentDelete, reqStudentList, reqStudentUpdate } from "@/api/student";
// 引入state返回对象类型
import type { StudentState } from "./types/type";
// 创建学生小仓库
let useStudentStore = defineStore('Student',{
    // 这里使用选择式Api写法
    // 存储数据位置
    // 指定state函数返回值类型为StudentState
    state:() : StudentState=>{
        return{
            students:[]
        }
    },
    // 异步|逻辑
    actions:{
        // 获取所有学生
        async getStudentAll(){
            let result:studentAllResponseData = await reqStudentAll();
            if(result.status == 0){
                this.students = (result.data as studentInfoData[])
                // 当前async函数返回一个成功的promise
                return "ok"
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        // 分页获取学生
        async getStudentList(data:studentListFormData){
            let result:studentListResponseData = await reqStudentList(data);
            if(result.status == 0){
                this.students = result.data?.data as studentInfoData[]
                // 当前async函数返回一个成功的promise
                return result.data
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        // 添加学生
        async addStudent(data:studentInfoData){
            let result:studentResponseData = await reqStudentAdd(data);
            if(result.status == 0){
                // 当前async函数返回一个成功的promise
                return "ok"
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        // 根据id查询学生
        async getStudentById(_id:string){
            let result:studentResponseData = await reqStudentById(_id);
            if(result.status == 0){
                // 当前async函数返回一个成功的promise
                return result.data
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        // 更新学生信息
        async updateStudent(data:studentInfoData){
            let result:studentResponseData = await reqStudentUpdate(data);
            if(result.status == 0){
                // 当前async函数返回一个成功的promise
                return "ok"
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        // 删除学生
        async deleteStudent(studentId:string){
            let result:studentResponseData = await reqStudentDelete(studentId);
            if(result.status == 0){
                // 当前async函数返回一个成功的promise
                return "ok"
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
    },
    // 计算属性
    getters:{

    }
})
// 对外暴露获取小仓库方法
export default useStudentStore